package com.openapi.protocol.encode;

import com.openapi.data.JT808PushData;
import exsun.jt808.data.base.JT808Category;
import exsun.jt808.data.distribute_data.DistributeEntity;
import exsun.jt808.data.distribute_data.jsatl12.AlarmAttachmentUpload;
import exsun.jt808.data.distribute_data.jsatl12.FileUploadFinishedRep;
import exsun.jt808.data.distribute_data.jt808.*;
import exsun.jt808.data.upload_data.jt808.LatLng;
import exsun.jt808.data.util.parse.BCD8421Operater;
import exsun.jt808.data.util.parse.BitOperator;
import exsun.jt808.data.util.parse.HexStrUtil;


public class PackageHj212 {
    /**
     * 报警附件上传指令
     */
    public static JT808PushData alarmAttachmentUploadEncode(AlarmAttachmentUpload data)
    {
        byte[] msgBodyBytes = BitOperator.concatAll(
                BitOperator.integerTo1Bytes(data.getIpaddress().length()),
                BitOperator.stringToBytes(data.getIpaddress()),
                BitOperator.integerTo2Bytes(data.getTcpPort()),
                BitOperator.integerTo2Bytes(data.getUdpPort()),
                HexStrUtil.hexToBytes(data.getAlarmFlag()),
                BitOperator.stringToBytes(BitOperator.padLeft(data.getAlarmCode(),32,'0')),
                BitOperator.stringToBytes("0000000000000000")
        );
        return new JT808PushData(0x9208, msgBodyBytes, data);
    }

    public static JT808PushData fileFinishedRepEncode(FileUploadFinishedRep data) {

        byte[] msgbody = BitOperator.concatAll(BitOperator.integerTo1Bytes(data.getFileName().length()),
                BitOperator.stringToBytes(data.getFileName()),
                BitOperator.integerTo1Bytes(data.getFileType()));
        int finished = 0;
        if(data.getMiss().size() > 0){
            finished = 1;
        }
        msgbody = BitOperator.concatAll(msgbody, BitOperator.integerTo1Bytes(finished),
                BitOperator.integerTo1Bytes(data.getMiss().size()));

        for (Integer key : data.getMiss().keySet()) {
            msgbody = BitOperator.concatAll(msgbody, BitOperator.integerTo4Bytes(key),
                    BitOperator.integerTo4Bytes(data.getMiss().get(key)));
        }
        JT808PushData JT808PushData = new JT808PushData(0x9212,msgbody,data, JT808Category.platformReplay);
        return JT808PushData;
    }

}
